package com.skq.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.skq.mapper.ArticleMapper;
import com.skq.pojo.Article;
import com.skq.pojo.PageBean;
import com.skq.service.ArticleService;
import com.skq.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author skq
 * @data 2024/6/11
 */
@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public void add(Article article) {
        //补充属性值
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        article.setCreateUser(id);

        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());

        articleMapper.add(article);
    }

    @Override
    public PageBean<Article> list(Integer pageNum, Integer pageSize, String categoryId, String state) {
        PageBean<Article> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");

        Page<Article> page = articleMapper.list(userId, categoryId, state);

        pb.setTotal(page.getTotal());
        pb.setItems(page.getResult());

        return pb;
    }
}
